package Pool;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolDemo {
    public static void main(String[] args) {
        int exec = 4;
        switch(exec){
            case 1:
            // 大小为 5 的线程池
            ExecutorService threadpool1 = Executors.newFixedThreadPool(5);
            try {
                for (int i = 0; i < 10; i++) {
                    threadpool1.execute(()->{
                        System.out.println(Thread.currentThread().getName()+" 进入线程池");
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        };
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
                threadpool1.shutdown();
            }
            break;

            case 2:
            // 单线程池
            ExecutorService threadpool2 = Executors.newSingleThreadExecutor();
            try {
                for (int i = 0; i < 10; i++) {
                    threadpool2.execute(()->{
                        System.out.println(Thread.currentThread().getName()+" 进入线程池");
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        };
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }  finally{
                threadpool2.shutdown();
            }
            break;

            case 3:
            // 可扩容线程池，动态
            ExecutorService threadpool3 = Executors.newCachedThreadPool();
            try {
                for (int i = 0; i < 5; i++) {
                    threadpool3.execute(()->{
                        System.out.println(Thread.currentThread().getName()+" 进入线程池");
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        };
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }  finally{
                threadpool3.shutdown();
            }
            break;

            case 4:
            // 更底层
            // 自定义线程池
            ExecutorService threadpool4 = new ThreadPoolExecutor(
                2, // 常驻线程数
                5, // 线程池最大线程数
                2, // 存活时间
                TimeUnit.NANOSECONDS, // 存活时间单位 
                new ArrayBlockingQueue<>(3), // 阻塞队列
                Executors.defaultThreadFactory(), // 线程工厂
                new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略
            try {
                for (int i = 0; i < 8; i++) {
                    threadpool4.execute(()->{
                        System.out.println(Thread.currentThread().getName()+" 进入线程池");
                        try {
                            Thread.sleep(30000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        };
                    });
                }
            } catch (Exception e) {
                // e.printStackTrace();
                System.out.println("无法接收任务...");
            }  finally{
                threadpool4.shutdown();
            }
            break;
        }
        
        
        
    }
}
